package com.lightboat.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lightboat.common.constant.CacheConstants;
import com.lightboat.common.utils.RedisUtils;
import com.lightboat.system.domain.CrmCity;
import com.lightboat.system.domain.vo.CrmCityTreeVo;
import com.lightboat.system.mapper.CrmCityMapper;
import com.lightboat.system.service.CrmCityAsyncService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CrmCityAsyncServiceImpl extends ServiceImpl<CrmCityMapper, CrmCity> implements CrmCityAsyncService {


    @Resource
    private RedisUtils redisUtil;

    /**
     * 设置属性列表进入redis
     */
    @Override
    public void setListTree() {
        // 循环数据，把数据对象变成带list结构的vo
        List<CrmCityTreeVo> treeList = new ArrayList<>();

        LambdaQueryWrapper<CrmCity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // lambdaQueryWrapper.select(CrmCity::getId, CrmCity::getCityId, CrmCity::getParentId, CrmCity::getName);
        lambdaQueryWrapper.eq(CrmCity::getIsShow, true);
        List<CrmCity> allTree = baseMapper.selectList(lambdaQueryWrapper);
        if (CollUtil.isEmpty(allTree)) {
            return;
        }

        for (CrmCity crmCity : allTree) {
            CrmCityTreeVo crmCityTreeVo = new CrmCityTreeVo();
            BeanUtils.copyProperties(crmCity, crmCityTreeVo);
            treeList.add(crmCityTreeVo);
        }
        // 返回
        Map<Long, CrmCityTreeVo> map = new HashMap<>();
        // cityId 为 key 存储到map 中
        for (CrmCityTreeVo crmCityTreeVo1 : treeList) {
            map.put(crmCityTreeVo1.getCityId(), crmCityTreeVo1);
        }
        List<CrmCityTreeVo> list = new ArrayList<>();
        for (CrmCityTreeVo tree : treeList) {
            // 子集ID返回对象，有则添加。
            CrmCityTreeVo tree1 = map.get(tree.getParentId());
            if (tree1 != null) {
                tree1.getChild().add(tree);
            } else {
                list.add(tree);
            }
        }
        redisUtil.set(CacheConstants.CITY_LIST_TREE, list);
    }

    /**
     * 数据整体刷入redis
     */
    private void asyncList(Long pid) {
        LambdaQueryWrapper<CrmCity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CrmCity::getParentId, pid);
        lambdaQueryWrapper.in(CrmCity::getIsShow, true);
        List<CrmCity> systemCityList = baseMapper.selectList(lambdaQueryWrapper);
        if (systemCityList != null && !systemCityList.isEmpty()) {
            redisUtil.hset(CacheConstants.CITY_LIST, pid.toString(), systemCityList);
        }
    }

    /**
     * 数据整体刷入redis
     */
    @Async
    public void async(Long pid) {
        asyncList(pid);
        setListTree();
    }

    @Async
    public void async(Long[] pids) {
        for (Long pid : pids) {
            asyncList(pid);
        }
        setListTree();
    }
}

